package ikar.view.components;

import ikar.nrd.fillingref.generator.XsltInTemplates;
import ikar.nrd.model.FillingRef;
import ikar.nrd.model.XsltNodeType;

import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.panel.Panel;

public class XslInTrasformPanel extends Panel {

	private static final long serialVersionUID = 1L;
	private final String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
			+ "<xsl:stylesheet version=\"2.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" \n"
			+ "xmlns:rk=\"http://www.fpml.org/FpML-5/recordkeeping\" xmlns:nsdext=\"http://www.fpml.org/FpML-5/recordkeeping/nsd-ext\"	"
			+ "xmlns:nsd=\"http://www.fpml.org/FpML-5/recordkeeping\"><xsl:output omit-xml-declaration=\"yes\" indent=\"yes\" />\n";

	public XslInTrasformPanel(String id, DefaultMutableTreeNode rootNode) {
		super(id);
		String content = XsltInTemplates.getTradeContent(getXsltContent(rootNode));
		add(new MultiLineLabel("multiLineLabel", content).setEscapeModelStrings(true));

	}

	private String getXsltContent(DefaultMutableTreeNode rootNode) {
		DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) rootNode.getRoot();
		FillingRef fillRef = ((FillingRef) parentNode.getUserObject());

		// StringBuilder strBuilder = new StringBuilder("<xsl:template match=\"nsd:" + fillRef.getXpath() + "\">\n<MAIN>\n<DOC_TYPE>DOCUMENT_TYPE</DOC_TYPE>\n<SOURCE>NSD</SOURCE>\n");
		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append(getNodesIerarchy(rootNode, null));
		// strBuilder.append("</MAIN>\n</xsl:template>\n</xsl:stylesheet>");
		return strBuilder.toString();
	}

	private StringBuilder getNodesIerarchy(DefaultMutableTreeNode rootNode, FillingRef foreachNode) {
		StringBuilder strBuilder = new StringBuilder();

		for (int i = 0; i < rootNode.getChildCount(); i++) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) rootNode.getChildAt(i);
			FillingRef fillRef = ((FillingRef) node.getUserObject());

			if (!fillRef.isSelected()) {
				strBuilder.append(getNodesIerarchy(node, foreachNode));
				continue;
			}
			if (XsltNodeType.FOREACH.getName().equals(fillRef.getXsltNodeType())) {
				strBuilder.append("<xsl:for-each select=\"" + fillRef.getFullPath() + "\">\n");
				strBuilder.append("<" + fillRef.getPlateCode() + ">\n");
				strBuilder.append(getNodesIerarchy(node, fillRef));
				strBuilder.append("</" + fillRef.getPlateCode() + ">\n</xsl:for-each>\n");
			} else {
				strBuilder.append("\n");
				strBuilder.append("<" + fillRef.getPlateCode() + ">\n");
				strBuilder.append("    <xsl:value-of select=\"" + getXpathValue(fillRef, foreachNode) + "\" />\n");
				strBuilder.append("</" + fillRef.getPlateCode() + ">\n");
				strBuilder.append(getNodesIerarchy(node, foreachNode));
				strBuilder.append("\n");
			}

		}
		return strBuilder;
	}

	private String getXpathValue(FillingRef fillRef, FillingRef foreachNode) {
		if (foreachNode != null) {
			return fillRef.getFullPath().replace(foreachNode.getFullPath() + "/", "");
		}
		return fillRef.getFullPath();
	}
}
